<%@ page import="cn.hutool.core.util.ArrayUtil" %>
<%@ page import="java.util.List" %>
<%@ page import="cn.hutool.core.util.StrUtil" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
    <title>文件上传下载</title>
</head>
<body>

<h1>文件列表</h1>
<form action="index" method="GET">
    <label for="fileName">Enter File Name:</label>
    <input type="text" id="fileName" name="fileName" required>
    <button type="submit">Search</button>
</form>
<table border="1">
    <thead>
    <tr>
        <th>文件名</th>
        <th>上传日期</th>
        <th>下载</th>
        <th>删除</th>
    </tr>
    </thead>
<%
    int currentPage = 0;
    int totalPages = 0;
    if (request.getAttribute("currentPage")!=null){
         currentPage = (int) request.getAttribute("currentPage");
         totalPages = (int) request.getAttribute("totalPages");
    }
    List<String> list1 = (List<String>) request.getAttribute("nameList");
    List<String> list2 = (List<String>) request.getAttribute("dataList");
    List<String> list3 = (List<String>) request.getAttribute("idList");


%>
<%  if (ArrayUtil.isNotEmpty(list1)){
    for (int i = 0; i<list1.size(); i++) {
%>
    <tr>
        <td>
            <a><%=list1.get(i)%></a> &nbsp;&nbsp;
        </td>
        <td>
            <a><%=list2.get(i)%></a> &nbsp;&nbsp;
        </td>
        <td>
<%--            通过下载的servlet下载文件--%>
<%--            <a href="/download?name=<%=list1.get(i)%>">下载</a>--%>
<%--    通过文件的url下载文件--%>
            <a href="/resourse/<%=list1.get(i)%>" download="<%=list1.get(i)%>">下载</a>
        </td>
        <td>
<%--            通过删除的servlet删除文件，传入文件的id 删除数据库的数据--%>
            <a href="/delete?shan=<%=list3.get(i)%>&name=<%=list1.get(i)%>">删除</a>
        </td>
    </tr>
<%
    }
    }
%>
</table>

<p>
    第<%=currentPage %> 页 / 共 <%=totalPages%> 页

        <% int shang = currentPage-1;
   if (shang ==0 ){
       shang=1;
   }
   int xia = currentPage+1;
   if (xia == totalPages+1 ){
       xia = totalPages;
   }
%>
    <a href="/index?page=<%=shang%>">上一页</a>
    <a href="/index?page=<%=xia%>">下一页</a>
</p>
<hr>
<h2>文件上传</h2>
<form action="upload" method="post" enctype="multipart/form-data">
    <input type="file" name="file">
    <button>上传</button>
</form>
</body>
</html>
